package com.mall.shopping.services.categories;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dto.AllProductCateRequest;
import com.mall.shopping.dto.AllProductCateResponse;
import com.mall.shopping.dto.ProductCateDto;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class IProductCateServiceImpl implements IProductCateService {

    @Autowired
    ItemCatMapper itemCatMapper;
    @Autowired
    ProductCateConverter productCateConverter;

    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {

        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        try {
            List<ItemCat> itemCats = itemCatMapper.selectAll();
            //转化对象
            List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
            //转化成功
            allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            allProductCateResponse.setProductCateDtoList(productCateDtos);

        }catch (Exception e){
            e.printStackTrace();
            allProductCateResponse.setCode(ShoppingRetCode.DB_EXCEPTION.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.DB_EXCEPTION.getMessage());
        }

        return allProductCateResponse;
    }
}
